package seleniumdemo.web.impl;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import seleniumdemo.common.driver.DriverBase;
import seleniumdemo.common.driver.Wait;
import seleniumdemo.web.TestEIPLoginWeb;
import seleniumdemo.web.ContractWeb;

import java.util.List;

/**
 * @author guoying
 * @version 1.0
 * @date 2021/8/1 15:45
 */
public class ContractWebImpl implements ContractWeb {

   private TestEIPLoginWeb loginService =new TestEIPLoginWebImpl();



    @Override
    public Boolean openContractMenu(Wait wait) {
        DriverBase driverBase=wait.getDriverBase();

        try {
            boolean  titleIs=  wait.titleIs("供应链协同平台");
        } catch (Exception e) {
            System.err.println("用户没有登录或网址输入错误！");
            return false;
        }
        //等待5s，等待过程中如果定位到元素，就直接执行后续的代码，反之等待10s后报错误信息
        By by=By.xpath("//*[@id=\"root\"]/div/section/aside/div/div[2]/ul/li[10]");
        WebElement element=null;
        try{
            element=wait.visibility(by);
            element=wait.driverWaitClick(by);
            element.click();
        } catch (Exception e) {
            System.err.println("该用户没有供应链管理系统的权限！");
            return false;
        }


        //供应商管理  //div[contains(@data-menu-id,'SCM/VendorManager')
        By bySecondary= By.xpath("//*[@id=\"root\"]/div/section/aside/div/div[2]/ul/li[10]//ul//li[3]");
        try {
            element=wait.driverWaitClick(bySecondary);
            element.click();
        }catch (Exception e) {
            System.err.println("该用户没有供应商管理的权限！");
            return false;
        }


        //属地合同管理//input[contains(@name,'na')]
        By by3 = By.xpath("//*[@id=\"root\"]/div/section/aside/div/div[2]/ul/li[10]//ul//li[3]//ul//li[6]");
        try {
            element=wait.driverWaitClick(by3);
            element.click();
        }catch (Exception e) {
            System.err.println("该用户没有属地合同管理的权限！");
            return false;
        }
        return true;

    }


    //属地合同管理列表页按照合同编码搜索合同
    public Boolean searchByContractCode(String contractCode,Wait wait){
        DriverBase driverBase=wait.getDriverBase();
        WebElement element=null;
        //等待元素点击
        try {
            element=wait.driverWaitClick(By.xpath("//*[@id=\"SearchConditionForm\"]/table/tbody/tr[1]/td[2]/span/input[1]"));
        } catch (Exception e) {
            System.err.println("该页面不能输入合同编码");
            return false;
        }
        //点击输入合同编码
        element.click();
        element.clear();
        element.sendKeys(contractCode);
        //点击搜索
        driverBase.findElement(By.xpath("//*[@id=\"QueryConditionSearch\"]")).click();
        By xpath = By.xpath("//tr[@id=\"datagrid-row-r1-2-0\"]/td[3]/div");
        //等待元素的值是否符合预期
        try {
            Boolean aBoolean = wait.textIs(xpath, contractCode);
            return aBoolean;
        }catch (Exception e) {
            System.err.println("搜索失败");
            return false;
        }

    }

    @Override
    public void contractCopy(String beginTime, String endTime,Wait wait,String contractCode,String newContractCode) {
        DriverBase driverBase=wait.getDriverBase();
        //切换到当前菜单的iframe   <iframe name="https://testeip.colipu.com" src="https://testscm.colipu.com/Contract/OutsourceContract/Index?undefined" scrolling="auto" frameborder="0" style="min-height: 500px; height: 100%; width: 100%;"></iframe>
        driverBase.getDriver().switchTo().frame("https://testeip.colipu.com");
        WebElement element=null;
        if(searchByContractCode(newContractCode,wait)){
            System.out.println("合同编码已存在,复制失败,请修改合同编码.");
            return;
        }
        if(!searchByContractCode(contractCode,wait)){
            System.out.println("没有搜索到需要复制的合同.");
            return;
        }

        //点击复制按钮
        By by=By.xpath("//*[@id=\"datagrid-row-r1-2-0\"]/td[22]/div/span[2]/i[@class=\"glyphicon icon-copy\"]");
        try {
            element = wait.driverWaitClick(by);
        }catch (Exception e) {
            System.err.println("没有合同复制的权限");
            return;
        }
        element.click();

        //从iframe中切出
        driverBase.getDriver().switchTo().defaultContent();

        //  //*[@id="rc-tabs-0-panel-OutsourceContractCopy"]
        //切换到合同复制的iframe
        WebElement copyIframe=driverBase.findElement(By.xpath("//div[@id=\"rc-tabs-0-panel-OutsourceContractCopy\"]/iframe"));
        driverBase.getDriver().switchTo().frame(copyIframe);
        WebElement element1=null;
        //修改合同编码
        WebElement element3=driverBase.findElement(By.xpath("//*[@id=\"OutsourceContractForm\"]/table/tbody/tr[2]/td[2]/span/input[1]"));
        element3.clear();
        element3.sendKeys(newContractCode);
        //修改日期
        try {
            element1 = wait.driverWaitClick(By.xpath("//*[@id=\"OutsourceContractForm\"]/table/tbody/tr[2]/td[4]/span/input[1]"));
        } catch (Exception e) {
            System.err.println("输入合同起始日期报错！");
            return;
        }
        JavascriptExecutor js =(JavascriptExecutor)driverBase.getDriver();
        Actions action = new Actions(driverBase.getDriver());
        WebElement element2=null;
        try {
            element2 = wait.driverWaitClick(By.xpath("//*[@id=\"OutsourceContractForm\"]/table/tbody/tr[2]/td[6]/span/input[1]"));
        } catch (Exception e) {
            System.err.println("输入合同结束日期报错！");
            return;
        }
        js.executeScript("arguments[0].removeAttribute('readonly');arguments[1].removeAttribute('readonly')", element1,element2);
        element1.click();
        element1.clear();
        element1.sendKeys(beginTime);
        element2.click();
        element2.clear();
        element2.sendKeys(endTime);
        element1.click();
        action.sendKeys(Keys.TAB).perform();
        element1.click();
        action.sendKeys(Keys.ENTER).perform();
        element2.click();
        action.sendKeys(Keys.TAB).perform();
        element2.click();
        action.sendKeys(Keys.ENTER).perform();

        //点击保存
        driverBase.findElement(By.xpath("//*[@id=\"OutsourceContractSave\"]")).click();
        //切换iframe到属地合同管理
        driverBase.getDriver().switchTo().defaultContent();
        driverBase.getDriver().switchTo().frame(driverBase.findElement(By.xpath("//*[@id=\"rc-tabs-0-panel-SCM/VendorManager/OutsourceContract\"]/iframe")));
        if(searchByContractCode(newContractCode,wait)){
            System.out.println("属地合同复制成功");
        }else {
            System.out.println("属地合同复制失败,请手动复制");
        }
    }
}
